package com.github.pig.sales.controller;
import java.util.Map;
import java.util.Date;

import com.github.pig.common.util.UserUtils;
import com.github.pig.common.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.pig.common.constant.CommonConstant;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.github.pig.common.util.Query;
import com.github.pig.common.util.R;
import com.github.pig.sales.entity.SalesPublicRepertory;
import com.github.pig.sales.service.ISalesPublicRepertoryService;
import com.github.pig.common.web.BaseController;

/**
 * <p>
 * 公共库配置 前端控制器
 * </p>
 *
 * @author andrew
 * @since 2018-06-16
 */
@RestController
@RequestMapping("/salesPublicRepertory")
public class SalesPublicRepertoryController extends BaseController {
    @Autowired private ISalesPublicRepertoryService salesPublicRepertoryService;

    /**
    * 通过ID查询
    *
    * @param id ID
    * @return SalesPublicRepertory
    */
    @GetMapping("/{id}")
    public R<SalesPublicRepertory> get(@PathVariable Integer id) {
        return new R<>(salesPublicRepertoryService.selectById(id));
    }


    /**
    * 分页查询信息
    *
    * @param params 分页对象
    * @return 分页对象
    */
    @RequestMapping("/page")
    public Page page(@RequestParam Map<String, Object> params) {
        params.put(CommonConstant.DEL_FLAG, CommonConstant.STATUS_NORMAL);
        return salesPublicRepertoryService.selectPage(new Query<>(params), new EntityWrapper<>());
    }

    /**
     * 添加
     * @param  salesPublicRepertory  实体
     * @return success/false
     */
    @PostMapping
    public R<Boolean> add(@RequestBody SalesPublicRepertory salesPublicRepertory, UserVO userVO) {
        salesPublicRepertory.setUpdateTime(new Date());
        salesPublicRepertory.setDelFlag(CommonConstant.STATUS_NORMAL);
        salesPublicRepertory.setVersion(CommonConstant.FIRST_VERSION);
        salesPublicRepertory.setUpdateUser(userVO.getUserId());
        salesPublicRepertory.setUpdateUserName(userVO.getUsername());
        return new R<>(salesPublicRepertoryService.insert(salesPublicRepertory));
    }

    /**
     * 删除
     * @param id ID
     * @return success/false
     */
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable Integer id, UserVO userVO) {
        SalesPublicRepertory salesPublicRepertory = new SalesPublicRepertory();
        salesPublicRepertory.setFid(id);
        salesPublicRepertory.setUpdateTime(new Date());
        salesPublicRepertory.setDelFlag(CommonConstant.STATUS_DEL);
        salesPublicRepertory.setUpdateUser(userVO.getUserId());
        salesPublicRepertory.setUpdateUserName(userVO.getUsername());
        return new R<>(salesPublicRepertoryService.updateById(salesPublicRepertory));
    }

    /**
     * 编辑
     * @param  salesPublicRepertory  实体
     * @return success/false
     */
    @PutMapping
    public R<Boolean> edit(@RequestBody SalesPublicRepertory salesPublicRepertory, UserVO userVO) {
        salesPublicRepertory.setUpdateTime(new Date());
        salesPublicRepertory.setUpdateUser(userVO.getUserId());
        salesPublicRepertory.setUpdateUserName(userVO.getUsername());
        return new R<>(salesPublicRepertoryService.updateById(salesPublicRepertory));
    }
}
